/**
 * 
 */
package com.cmcc.awareness;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.pcap4j.packet.Packet;
import org.pcap4j.packet.TcpPacket;
import org.pcap4j.packet.UdpPacket;

import jpcap.packet.TCPPacket;
import jpcap.packet.UDPPacket;

/**
 * @author Amish
 * @date 2018年12月7日 下午7:14:09
 * 
 * 端口表达式：PORT
 * 
 * eg
 * PORT:19100|19101|19102|0
 * 
 * eg
 * PORT:19100|19101|19102|1
 */
public class ProtoDatProtocolFeatureCodeElement_PORT implements ProtoDatProtocolFeatureCodeElement {	
	private static Logger logger = LogManager.getLogger(ProtoDatProtocolFeatureCodeElement_PORT.class);

	String initialStr;
	String ports[];
	
	public String appName;
	public String protocolType;
	public ProtoDatProtocolFeatureCodeElement_PORT(String appName,String protocolType){
		this.appName = appName;
		this.protocolType = protocolType;
	}
	
	@Override
	public String getType() {
		return "PORT";
	}


	@Override
	public boolean distinguish(String protocolType,jpcap.packet.Packet packet) {
		try{
			String packetSrcPort;
			String packetDstPort;
			if("TCP".equals(protocolType)){
			    TCPPacket tp = (TCPPacket) packet;
			    packetSrcPort = String.valueOf(tp.src_port);
			    packetDstPort =  String.valueOf(tp.dst_port);

			}else if("UDP".equals(protocolType)){
				UDPPacket up = (UDPPacket) packet;
				packetSrcPort = String.valueOf(up.src_port);
			    packetDstPort =  String.valueOf(up.dst_port);

			}else{
				return false;
			}
			
			if(ArrayUtils.contains(ports, packetSrcPort)){
				//System.out.println(initialStr);
				return true;
			}
			if(ArrayUtils.contains(ports, packetDstPort)){
				//System.out.println(initialStr);
				return true;
			}
			return false;
		}catch(Exception e){
			e.printStackTrace();
			System.out.println("Distinguish Exception -- <"+this.appName+">"+"<"+this.protocolType+"> : "+this.toString());
			return false;
		}

	}


	@Override
	public void parseSelf(String element) {
		try{			
			this.initialStr = element;
			element = element.substring(5);
			ports = element.split("\\|");//数组最后一个元素是0或1，这里可以暂不处理

		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public String toString(){
		return initialStr;
	}

	@Override
	public boolean distinguish(String protocolType, Packet packet) {
		try{
			String packetSrcPort;
			String packetDstPort;
			if("TCP".equals(protocolType)){
				TcpPacket tp = (TcpPacket) packet;
			    packetSrcPort = String.valueOf(tp.getHeader().getSrcPort());
			    packetDstPort =  String.valueOf(tp.getHeader().getDstPort());

			}else if("UDP".equals(protocolType)){
				UdpPacket up = (UdpPacket) packet;
				packetSrcPort = String.valueOf(up.getHeader().getSrcPort());
			    packetDstPort =  String.valueOf(up.getHeader().getDstPort());

			}else{
				return false;
			}
			
			if(ArrayUtils.contains(ports, packetSrcPort)){
				//System.out.println(initialStr);
				return true;
			}
			if(ArrayUtils.contains(ports, packetDstPort)){
				//System.out.println(initialStr);
				return true;
			}
			return false;
		}catch(Exception e){
			e.printStackTrace();
			System.out.println("Distinguish Exception -- <"+this.appName+">"+"<"+this.protocolType+"> : "+this.toString());
			return false;
		}
	}


}
